from flask import Blueprint
from db_init import db
from flask import request
from models.mmodels import Student
users = Blueprint('stu',__name__)
from sqlalchemy import func

@users.route('/index',methods=["GET"])
def age():
    #小于年龄
    stu_lt= Student.query.filter(Student.age.__lt__(20)).all()
    #大于年龄
    stu_gt = Student.query.filter(Student.age.__gt__(20)).all()
    print(f"小于年龄的有：{stu_lt},大于年龄岁的有：{stu_gt}")
    return 'ok'

@users.route('/names',methods=["GET"])
def stu_name():
    name = request.args.get('name')
    good = Student.query.filter_by(name=name).all()
    print(good)
    for i in good:
        print(i.name)
    return 'ok'

@users.route('/sushe',methods=["GET"])
def stu_sushe_num():
    d_num = request.args.get('num')
    ret = Student.query.filter_by(sushe=d_num).all()
    print(ret)
    for i in ret:
        print(i.sushe)
    return 'ok'

@users.route('/count',methods=["GET"])
def classmate_count():
    ret_s = db.session.query(func.count(Student.id)).all()
    ret_a = db.session.query(func.avg(Student.age)).all()
    return f"人数是{ret_s},平均年龄{ret_a}"